<template>
  <div>
    <div v-html="content">
    </div>
  </div>
</template>

<script>
import MarkdownIt from 'markdown-it';

export default {
  name: "MDViewer",
  props: {
    fileUrl: {
      type: String,
      required: false
    },
  },
  watch: {
    'fileUrl': {
      handler(val) {
        this.parseFile(val)
      }
    },
  },
  mounted() {
    this.parseFile(this.fileUrl)
  },
  data() {
    return {
      content: "",
    }
  },
  methods: {
    parseFile(link) {
      let _t = this
      let md = new MarkdownIt({
        html: false,
      })
      // 请求
      if (link.startsWith("http")) {
        fetch(link).then(res => {
          return res.text();
        }).then(data => {
          _t.content = md.render(data);
        })
        // 本地
      } else {
        _t.content = md.render(require("@/assets/" + link).default)
        console.log(_t.content)
      }
    }
  }
}
</script>

<style scoped>

</style>